package com.yinp.proapp.module.game.tanchishe;

public class ALinkList {
    Node first;
    Node last;
    int size;

    public void insertFirst(int x, int y) {
        size++;
        Node node = new Node(x, y);
        if (first == null) {
            first = node;
            last = first;
            return;
        }
        node.next = first;
        first.previous = node;
        first = node;
    }

    public void insertLast(int x, int y) {
        size++;
        Node node = new Node(x, y);
        if (first == null) {
            first = node;
            last = first;
            return;
        }
        node.previous = last;
        last.next = node;
        last = node;
    }

    public boolean isFirstRepetition() {
        Node node = first.next;
        while (node != null) {
            if (node.x == first.x && node.y == first.y) {
                return true;
            }
            node = node.next;
        }
        return false;
    }

    public void removeLast() {
        if (last == null) {
            return;
        }
        if (first.equals(last)) {
            first = null;
            last = null;
            return;
        }
        last = last.previous;
        last.next = null;
        size--;
    }

    public float[] getAll() {
        if (first == null) return null;
        Node curNode = first;
        float[] value = new float[size * 2];
        int i = 0;
        while (curNode != null) {
            value[i] = curNode.x;
            value[i + 1] = curNode.y;
            i += 2;
            curNode = curNode.next;
        }
        return value;
    }

    public boolean contains(int x, int y) {
        Node node = first;
        while (node != null) {
            if (node.x == x && node.y == y) {
                return true;
            }
            node = node.next;
        }
        return false;
    }

    public void clearAll() {
        first = null;
        last = null;
        size = 0;
    }

    public void traverse() {
        Node curNode = first;
        while (curNode != null) {
            System.out.print(curNode.x + "=" + curNode.y + "++");
            curNode = curNode.next;
        }
        System.out.println();
    }
}
